package com.bmf.module.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bmf.enums.AppHttpCodeEnum;
import com.bmf.module.rbac.entity.LoginUser;
import com.bmf.module.rbac.entity.RbacUser;
import com.bmf.module.rbac.service.RbacUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private RbacUserService userService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        LambdaQueryWrapper<RbacUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RbacUser::getUsername,s);
        RbacUser one = userService.getOne(wrapper);
        //判断是否查到用户  如果没查到抛出异常
        AppHttpCodeEnum.LOGIN_ERROR.assertIsTrue(!Objects.isNull(one));
        //返回用户信息
        // TODO 查询权限信息封装
        return new LoginUser(one,null);
    }
}